package com.lngsyz.ywtb.biz.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.ywtb.entity.LoginInfo;
import com.lngsyz.ywtb.entity.biz.AddElecOrder;
import com.lngsyz.ywtb.entity.biz.AddElecOrderInnerSeal;
import com.lngsyz.ywtb.entity.biz.AddPhyOrder;
import com.lngsyz.ywtb.entity.biz.AddPhyOrderInnerSeal;
import com.lngsyz.ywtb.entity.biz.DeliveryInfo;
import com.lngsyz.ywtb.entity.biz.TradeInfo;
import com.lngsyz.ywtb.entity.biz.ywtb.InAddYwtb;
import com.lngsyz.ywtb.entity.biz.ywtb.InAddYwtbInnerSeal;
import com.lngsyz.ywtb.entity.source1.DbDate;
import com.lngsyz.ywtb.entity.source1.GsYwtb;
import com.lngsyz.ywtb.entity.source1.GsYwtbSeal;
import com.lngsyz.ywtb.entity.source1.SystemCompany;
import com.lngsyz.ywtb.entity.source1.SystemCustom;
import com.lngsyz.ywtb.entity.source1.SystemSeal;
import com.lngsyz.ywtb.entity.source1.YwtbCityOrder;
import com.lngsyz.ywtb.entity.source1.YwtbCityOrderSeal;
import com.lngsyz.ywtb.entity.source2.TCompanyPrice;
import com.lngsyz.ywtb.enumeration.CertType;
import com.lngsyz.ywtb.enumeration.DeliveryMethod;
import com.lngsyz.ywtb.enumeration.Pay;
import com.lngsyz.ywtb.enumeration.PayChannel;
import com.lngsyz.ywtb.enumeration.SealMaterial;
import com.lngsyz.ywtb.enumeration.SealMaterialElec;
import com.lngsyz.ywtb.enumeration.SealMedia;
import com.lngsyz.ywtb.enumeration.SealStatus;
import com.lngsyz.ywtb.enumeration.SealType;
import com.lngsyz.ywtb.mapper.source1.YwtbCityOrderMapper;
import com.lngsyz.ywtb.pay.exception.RequestSystemException;
import com.lngsyz.ywtb.util.CommonUtil;
import com.lngsyz.ywtb.util.HttpUtils;
import com.lngsyz.ywtb.util.IDCardValidator;
import com.lngsyz.ywtb.util.StringUtil;

import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import net.sf.json.JSONObject;

@Service
public class YwtbCityOrderService {
    
	@Autowired
	YwtbCityOrderMapper ywtbCityOrderMapper;
	@Autowired
	YwtbCityOrderSealService ywtbCityOrderSealService;
	@Autowired
	SystemCompanyService systemCompanyService;
	@Autowired
	SystemCustomService systemCustomService;
	@Autowired
	SystemSealService systemSealService;
	@Autowired
	GsYwtbService gsYwtbService;
	@Autowired
	GsYwtbSealService gsYwtbSealService;
	@Autowired
	TCompanyPriceService tCompanyPriceService;
	
	public List<YwtbCityOrder> findByBusinessLicense(String businessLicense, String pay){
    	Map<String, Object> param = new HashMap<String, Object>();
    	if(StringUtils.isNotBlank(businessLicense)) {
    		param.put("businessLicense", businessLicense);
    		if(StringUtils.isNotBlank(pay)) {
    			param.put("pay", pay);
    		}
    		List<YwtbCityOrder> lists = ywtbCityOrderMapper.findByCondition(param);
    		return lists;
    	} else {
    		return new ArrayList<YwtbCityOrder>();
    	}
    }
	
	public List<YwtbCityOrder> findForPayStatusJob(){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("bz", "0");
		param.put("pay", Pay.PAY_NO.getKey());
		List<YwtbCityOrder> lists = ywtbCityOrderMapper.findByCondition(param);
		return lists;
	}
	
	public YwtbCityOrder findById(String id){
		if(StringUtils.isNotBlank(id)) {
			YwtbCityOrder yco = ywtbCityOrderMapper.findById(id);
			return yco;
		} else {
			return null;
		}
	}
	
	public Date selectDBDate() {
		DbDate dbDate = ywtbCityOrderMapper.selectCurrentDate();
		return dbDate.getCurrentDate();
	}
	
	public void updateOrder(YwtbCityOrder ywtbCityOrder) throws Exception {
		ywtbCityOrderMapper.updatePay(ywtbCityOrder);
	}
	
	public int updateBz(String id) {
    	Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		return ywtbCityOrderMapper.updateBz(param);
    }
	
	public DeliveryInfo packageDelivery(YwtbCityOrder yco) {
		DeliveryInfo rtn = new DeliveryInfo();
		rtn.setDeliveryMethod(DeliveryMethod.getNameByKey(yco.getDeliveryMethod()));
		rtn.setReceiveName(yco.getReceiveName());
		rtn.setReceivePhone(yco.getReceivePhone());
		rtn.setReceiveAddress(yco.getReceiveAddress());
		rtn.setReceiveCertNo(yco.getReceiveCertNo());
		return rtn;
	}
	
	public TradeInfo packageTrade(YwtbCityOrder yco) {
		TradeInfo rtn = new TradeInfo();
		rtn.setPay(Pay.getNameByKey(yco.getPay()));
		rtn.setTradeChannel(PayChannel.getNameByKey(yco.getPayChannel()));
		rtn.setTradeNo(yco.getTradeno());
		rtn.setGmtCreate(CommonUtil.dateToStrLong(yco.getCreateDate()));
		rtn.setGmtPay(CommonUtil.dateToStrLong(yco.getZfsj()));
		return rtn;
	}

	/**
	 * 
	 * @param ywtbCityOrder
	 * @throws Exception
	 */
	public void payDeal(YwtbCityOrder ywtbCityOrder) throws Exception {
		// 调用一网通办下订单接口
		InAddYwtb inAddYwtb = packageSendYwtb(ywtbCityOrder);
		String _host = "";
		String _port = "";
		Map<String, String> headers = new HashMap<String, String>();
		JSONObject bodys = JSONObject.fromObject(inAddYwtb);
		String requestResult = HttpUtils.doPost(_host, _port, headers, bodys);
		try{
			System.out.println(requestResult);
			if(StringUtils.isNotBlank(requestResult)) {
				JSONObject jo = JSONObject.fromObject(requestResult);
				String code = jo.containsKey("code") ? jo.getString("code") : "";
				if(StringUtils.equals(code, "1")) {
					// 如果成功处理，更新本地库
					updateBz(ywtbCityOrder.getId());
					ywtbCityOrderSealService.updateBz(ywtbCityOrder.getId());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private InAddYwtb packageSendYwtb(YwtbCityOrder ywtbCityOrder) {
		InAddYwtb rtn = new InAddYwtb();
		rtn.setBusinessId(ywtbCityOrder.getBusinessId());
		rtn.setPripid(ywtbCityOrder.getPripid());
		rtn.setTradeNo(ywtbCityOrder.getTradeno());
		rtn.setCompanyId(ywtbCityOrder.getCompanyId());
		rtn.setDeliveryMethod(ywtbCityOrder.getDeliveryMethod());
		rtn.setZfsj(CommonUtil.dateToStrLong(ywtbCityOrder.getZfsj()));
		rtn.setReceiveName(ywtbCityOrder.getReceiveName());
		rtn.setReceiveCertType("10");
		rtn.setReceiveCertNo(ywtbCityOrder.getReceiveCertNo());
		rtn.setReceiveAddress(ywtbCityOrder.getReceiveAddress());
		rtn.setReceivePhone(ywtbCityOrder.getReceivePhone());
		rtn.setName(ywtbCityOrder.getName());
		rtn.setAddress(ywtbCityOrder.getAddress());
		rtn.setInstCode(ywtbCityOrder.getInstCode());
		rtn.setCrpName(ywtbCityOrder.getCrpName());
		rtn.setCrpCertType("10");
		rtn.setCrpCertNo(ywtbCityOrder.getCrpCertNo());
		rtn.setCrpPhone(ywtbCityOrder.getCrpPhone());
		rtn.setApName(ywtbCityOrder.getApName());
		rtn.setApCertType("10");
		rtn.setApCertNo(ywtbCityOrder.getApCertNo());
		rtn.setApPhone(ywtbCityOrder.getApPhone());
		rtn.setDistrictCode(ywtbCityOrder.getDistrictCode());
		rtn.setCreateDate(CommonUtil.dateToStrLong(ywtbCityOrder.getCreateDate()));
		rtn.setGxsj(ywtbCityOrder.getGmtUpdate());
		rtn.setGov("");
		rtn.setESeal("");
		rtn.setId(ywtbCityOrder.getId());
		rtn.setTax("");
		List<YwtbCityOrderSeal> ywtbCityOrderSeals = ywtbCityOrderSealService.findByCondition(ywtbCityOrder.getId());
		List<InAddYwtbInnerSeal> seals = new ArrayList<InAddYwtbInnerSeal>();
		for(YwtbCityOrderSeal ywtbCityOrderSeal : ywtbCityOrderSeals) {
			InAddYwtbInnerSeal seal = new InAddYwtbInnerSeal();
			seal.setSealId(ywtbCityOrderSeal.getSealId());
			seal.setSealType(ywtbCityOrderSeal.getSealType());
			seal.setSealMedia(ywtbCityOrderSeal.getSealMedia());
			seal.setSealMaterial(ywtbCityOrderSeal.getSealMaterial());
			seal.setPrice(ywtbCityOrderSeal.getPrice());
			seal.setReferSealId(ywtbCityOrderSeal.getReferSealId());
			seals.add(seal);
		}
		rtn.setSeals(seals);
		return rtn;
	}
	
	public void addPhyOrderCheck(AddPhyOrder addPhyOrder, LoginInfo loginInfo) throws Exception {
		// gov : 政府买单标记 1：政府买单，不需要支付 0：政府不买单，需要支付
		// 当前大连，正常支付，线下买单，所以需要在线支付
    	if(StringUtils.isBlank(addPhyOrder.getCompanyId())) {
    		throw new RequestSystemException("刻章厂ID不可为空！");
    	} else {
    		SystemCompany systemCompany = systemCompanyService.findById(addPhyOrder.getCompanyId());
    		if(systemCompany == null) {
    			throw new RequestSystemException("信息有误（刻章厂）！");
    		}
    	}
    	if(StringUtils.isBlank(addPhyOrder.getDeliveryMethod())) {
    		throw new RequestSystemException("交付方式不可为空！");
    	} else {
    		if(StringUtils.isBlank(DeliveryMethod.getNameByKey(addPhyOrder.getDeliveryMethod()))) {
    			throw new RequestSystemException("信息有误（交付方式）！");
    		}
    	}
    	if(StringUtils.equals(addPhyOrder.getDeliveryMethod(), DeliveryMethod.MAIL.getKey())) {
    		// 如果邮寄，需要补充收货人信息
    		if(StringUtils.isBlank(addPhyOrder.getReceiveName())) {
    			throw new RequestSystemException("收货人姓名不可为空！");
        	}
    		if(StringUtils.isNotBlank(addPhyOrder.getReceiveCertNo())) {
    			if(!IDCardValidator.verifyCard(addPhyOrder.getReceiveCertNo())) {
    				throw new RequestSystemException("收货人证件号码不正确！");
    			}
    		}
    		if(StringUtils.isBlank(addPhyOrder.getReceiveAddress())) {
    			throw new RequestSystemException("收货人地址不可为空！");
    		}
    		if(StringUtils.isBlank(addPhyOrder.getReceivePhone())) {
    			throw new RequestSystemException("收货人手机号码不可为空！");
    		} else {
    			if(!StringUtil.isCorrectPhone(addPhyOrder.getReceivePhone())) {
    				throw new RequestSystemException("信息有误（收货人手机号码）！");
        		}
    		}
    	}
		int sealType01 = 0;
		for(AddPhyOrderInnerSeal seal : addPhyOrder.getSeals()) {
			// 物理印章验证
			if(StringUtils.isBlank(SealType.getNameByKey(seal.getSealType()))) {
				throw new RequestSystemException("印章类型错误!");
			}
			if(StringUtils.isBlank(SealMaterial.getNameByKey(seal.getSealMaterial()))) {
				throw new RequestSystemException("印章材质错误!");
			}
			if(StringUtils.equals(SealType.TYPE_01.getKey(), seal.getSealType())) {
				sealType01++;
			}
			if(sealType01 > 1) {
				throw new RequestSystemException("单位申请公章数量不可以大于1");
			}
		}
		if(sealType01 > 0) {
			// 本平台是否存在带公章的订单
			Map<String, Object> ycoParam = new HashMap<String, Object>();
			ycoParam.put("instCode", loginInfo.getInstCode());
			List<YwtbCityOrder> ycoList = ywtbCityOrderMapper.findByCondition(ycoParam);
			if(ycoList.size() > 0) {
				for(YwtbCityOrder yco : ycoList) {
					// 检查支付的/未支付的 订单，里面是否有公章申请
					if(StringUtils.equals(yco.getPay(), Pay.PAY_YES.getKey()) || StringUtils.equals(yco.getPay(), Pay.PAY_NO.getKey())) {
						List<YwtbCityOrderSeal> ycosList = ywtbCityOrderSealService.findByCondition(yco.getId());
						if(ycosList.size() > 0) {
							for(YwtbCityOrderSeal ycos : ycosList) {
								if(StringUtils.equals(SealMedia.PHYSICAL.getKey(), ycos.getSealMedia())
										&& StringUtils.equals(SealType.TYPE_01.getKey(), ycos.getSealType())) {
									throw new RequestSystemException("当前单位已存在申请公章的订单，无法重复申请");
								}
							}
						}
					}
				}
			}
			// 其他平台（省市场局）是否有申请公章
			List<GsYwtb> gyList = gsYwtbService.findByBusinessLicense(loginInfo.getInstCode());
			if(gyList.size() > 0) {
				for(GsYwtb gy : gyList) {
					if(!StringUtils.equals("1", gy.getRefund())) {
						List<GsYwtbSeal> gysList = gsYwtbSealService.findByMainId(gy.getId());
						for(GsYwtbSeal gys : gysList) {
							if(StringUtils.equals(SealMedia.PHYSICAL.getKey(), gys.getSealMedia())
									&& StringUtils.equals(SealType.TYPE_01.getKey(), gys.getSealType())) {
								throw new RequestSystemException("当前单位已存在申请公章的订单，无法重复申请");
							}
						}
					}
				}
			}
			// 系统内是否有公章
			SystemCustom sc = systemCustomService.findByBusinessLicenseForOne(loginInfo.getInstCode());
			if(sc != null) {
				String customId = sc.getId();
				String type = SealType.TYPE_01.getKey();
				List<SystemSeal> seals = systemSealService.findByCustomIdAndType(customId, type);
				if(seals.size() > 0) {
					for(SystemSeal seal : seals) {
						if(StringUtils.equals(SealType.TYPE_01.getKey(), seal.getType()) 
								&& StringUtils.equals(SealMedia.PHYSICAL.getKey(), seal.getMediatype())
								&& StringUtils.indexOf("1234", seal.getStatus()) > -1) {
							throw new RequestSystemException("当前单位已存在备案公章");
						}
					}
				}
			}
		}
	}
	
	public String addPhyOrder(AddPhyOrder addPhyOrder, LoginInfo loginInfo) throws Exception{
		// 信息入库到YWTB_CITY_ORDER
		YwtbCityOrder ywtbCityOrder = new YwtbCityOrder();
		String id = CommonUtil.createUuid();
		ywtbCityOrder.setBusinessId(loginInfo.getBusinessId());
		ywtbCityOrder.setPripid(loginInfo.getPripid());
		ywtbCityOrder.setCompanyId(addPhyOrder.getCompanyId());
		ywtbCityOrder.setTradeno(addPhyOrder.getTradeNo());
		ywtbCityOrder.setPay(Pay.PAY_NO.getKey());
		ywtbCityOrder.setPayChannel("wxappletpay");
		// ywtbCityOrder.setTotal(total);
		ywtbCityOrder.setDeliveryMethod(addPhyOrder.getDeliveryMethod());
		//ywtbCityOrder.setZfsj(zfsj);
		ywtbCityOrder.setReceiveName(addPhyOrder.getReceiveName());
		ywtbCityOrder.setReceiveCertType(CertType.SFZ.getKey());
		ywtbCityOrder.setReceiveCertNo(addPhyOrder.getReceiveCertNo());
		ywtbCityOrder.setReceiveAddress(addPhyOrder.getReceiveAddress());
		ywtbCityOrder.setReceivePhone(addPhyOrder.getReceivePhone());
		ywtbCityOrder.setName(loginInfo.getName());
		ywtbCityOrder.setAddress(loginInfo.getAddress());
		ywtbCityOrder.setInstCode(loginInfo.getInstCode());
		ywtbCityOrder.setCrpName(loginInfo.getCrpName());
		ywtbCityOrder.setCrpCertType(CertType.SFZ.getKey());
		ywtbCityOrder.setCrpCertNo(loginInfo.getCrpCertNo());
		ywtbCityOrder.setCrpPhone(loginInfo.getCrpPhone());
		ywtbCityOrder.setApName(loginInfo.getApName());
		ywtbCityOrder.setApCertType(CertType.SFZ.getKey());
		ywtbCityOrder.setApCertNo(loginInfo.getApCertNo());
		ywtbCityOrder.setDistrictCode(loginInfo.getDistrictCode());
		ywtbCityOrder.setCreateDate(loginInfo.getCreateDate());
		ywtbCityOrder.setBz("0");
		ywtbCityOrder.setId(id);
		ywtbCityOrder.setGov("0");
		ywtbCityOrder.setESeal("0");
		//ywtbCityOrder.setGmtCreate();
		ywtbCityOrder.setCreateApp(loginInfo.getAppId());
		//ywtbCityOrder.setGmtUpdate();
		
		BigDecimal total = BigDecimal.valueOf(0);
		List<YwtbCityOrderSeal> ywtbCityOrderSeals = new ArrayList<YwtbCityOrderSeal>();
		List<TCompanyPrice> tCompanyPrices = tCompanyPriceService.findByCompanyId(addPhyOrder.getCompanyId(), SealMedia.PHYSICAL.getKey());
		if(tCompanyPrices != null && tCompanyPrices.size() > 0) {
			TCompanyPrice tCompanyPrice = tCompanyPrices.get(0);
			for(AddPhyOrderInnerSeal innerSeal : addPhyOrder.getSeals()) {
				// 信息入库到YWTB_CITY_ORDER_SEAL
				YwtbCityOrderSeal ywtbCityOrderSeal = new YwtbCityOrderSeal();
				ywtbCityOrderSeal.setSealId(CommonUtil.createUuid());
				ywtbCityOrderSeal.setPripid(loginInfo.getPripid());
				ywtbCityOrderSeal.setSealType(innerSeal.getSealType());
				ywtbCityOrderSeal.setSealMedia(SealMedia.PHYSICAL.getKey());
				ywtbCityOrderSeal.setSealMaterial(innerSeal.getSealMaterial());
				// 存储价格
				BigDecimal price = BigDecimal.ZERO;
				if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_01.getKey())) {
					price = tCompanyPrice.getSeal01();
				} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_02.getKey())) {
					price = tCompanyPrice.getSeal02();
				} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_03.getKey())) {
					price = tCompanyPrice.getSeal03();
				} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_04.getKey())) {
					price = tCompanyPrice.getSeal04();
				} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_05.getKey())) {
					price = tCompanyPrice.getSeal05();
				} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_11.getKey())) {
					price = tCompanyPrice.getSeal06();
				}
				//ywtbCityOrderSeal.setPrice(innerSeal.getPrice());
				ywtbCityOrderSeal.setPrice(price);
				ywtbCityOrderSeal.setBz("0");
				//ywtbCityOrderSeal.setGmtCreate(gmtCreate);
				ywtbCityOrderSeal.setId(CommonUtil.createUuid());
				ywtbCityOrderSeal.setMainId(id);
				// 电子印章有
				//ywtbCityOrderSeal.setReferSealId();
				ywtbCityOrderSeals.add(ywtbCityOrderSeal);
				total = total.add(price);
			}
		}
		ywtbCityOrder.setTotal(total);
		
		ywtbCityOrderSealService.inserts(ywtbCityOrderSeals);
		ywtbCityOrderMapper.insert(ywtbCityOrder);
		return id;
	}

	public void addElecOrderCheck(AddElecOrder addElecOrder, LoginInfo loginInfo) throws Exception {
		// gov : 政府买单标记 1：政府买单，不需要支付 0：政府不买单，需要支付
		// 当前大连，正常支付，线下买单，所以需要在线支付
    	if(StringUtils.isBlank(addElecOrder.getCompanyId())) {
    		throw new RequestSystemException("刻章厂ID不可为空！");
    	} else {
    		SystemCompany systemCompany = systemCompanyService.findById(addElecOrder.getCompanyId());
    		if(systemCompany == null) {
    			throw new RequestSystemException("信息有误（刻章厂）！");
    		}
    	}
    	if(StringUtils.isBlank(addElecOrder.getDeliveryMethod())) {
    		throw new RequestSystemException("交付方式不可为空！");
    	} else {
    		if(StringUtils.isBlank(DeliveryMethod.getNameByKey(addElecOrder.getDeliveryMethod()))) {
    			throw new RequestSystemException("信息有误（交付方式）！");
    		}
    	}
    	if(StringUtils.equals(addElecOrder.getDeliveryMethod(), DeliveryMethod.MAIL.getKey())) {
    		// 如果邮寄，需要补充收货人信息
    		if(StringUtils.isBlank(addElecOrder.getReceiveName())) {
    			throw new RequestSystemException("收货人姓名不可为空！");
        	}
    		if(StringUtils.isNotBlank(addElecOrder.getReceiveCertNo())) {
    			if(!IDCardValidator.verifyCard(addElecOrder.getReceiveCertNo())) {
    				throw new RequestSystemException("收货人证件号码不正确！");
    			}
    		}
    		if(StringUtils.isBlank(addElecOrder.getReceiveAddress())) {
    			throw new RequestSystemException("收货人地址不可为空！");
    		}
    		if(StringUtils.isBlank(addElecOrder.getReceivePhone())) {
    			throw new RequestSystemException("收货人手机号码不可为空！");
    		} else {
    			if(!StringUtil.isCorrectPhone(addElecOrder.getReceivePhone())) {
    				throw new RequestSystemException("信息有误（收货人手机号码）！");
        		}
    		}
    	}
		int sealType01 = 0;
		for(AddElecOrderInnerSeal seal : addElecOrder.getSeals()) {
			// 物理印章验证
			if(StringUtils.isBlank(SealType.getNameByKey(seal.getSealType()))) {
				throw new RequestSystemException("印章类型错误!");
			}
			if(StringUtils.isBlank(SealMaterialElec.getNameByKey(seal.getSealMaterial()))) {
				throw new RequestSystemException("印章材质错误!");
			}
			if(StringUtils.isBlank(seal.getReferSealId())) {
				throw new RequestSystemException("关联物理印章ID不可为空");
			} else {
				SystemSeal systemSeal = systemSealService.findById(seal.getReferSealId());
				if(systemSeal != null) {
					if(StringUtils.equals(systemSeal.getStatus(), SealStatus.DELIVERY.getKey())) {
						String customId = systemSeal.getCustomid();
						SystemCustom systemCustom = systemCustomService.findById(customId);
						if(systemCustom != null) {
							if(!StringUtils.equals(systemCustom.getBusinesslicense(), loginInfo.getInstCode())) {
								throw new RequestSystemException("关联物理印章与当前企业信息不一致");
							}
						} else {
							// TODO
						}
					} else {
						throw new RequestSystemException("关联物理印章未交付");
					}
				} else {
					throw new RequestSystemException("未找到关联物理印章");
				}
			}
			if(StringUtils.equals(SealType.TYPE_01.getKey(), seal.getSealType())) {
				sealType01++;
			}
			if(sealType01 > 1) {
				throw new RequestSystemException("单位申请公章数量不可以大于1");
			}
		}
		if(sealType01 > 0) {
			// 本平台是否存在带公章的订单
			Map<String, Object> ycoParam = new HashMap<String, Object>();
			ycoParam.put("instCode", loginInfo.getInstCode());
			List<YwtbCityOrder> ycoList = ywtbCityOrderMapper.findByCondition(ycoParam);
			if(ycoList.size() > 0) {
				for(YwtbCityOrder yco : ycoList) {
					// 检查支付的/未支付的 订单，里面是否有公章申请
					if(StringUtils.equals(yco.getPay(), Pay.PAY_YES.getKey()) || StringUtils.equals(yco.getPay(), Pay.PAY_NO.getKey())) {
						List<YwtbCityOrderSeal> ycosList = ywtbCityOrderSealService.findByCondition(yco.getId());
						if(ycosList.size() > 0) {
							for(YwtbCityOrderSeal ycos : ycosList) {
								if(StringUtils.equals(SealMedia.ELEC.getKey(), ycos.getSealMedia())
										&& StringUtils.equals(SealType.TYPE_01.getKey(), ycos.getSealType())) {
									throw new RequestSystemException("当前单位已存在申请公章的订单，无法重复申请");
								}
							}
						}
					}
				}
			}
			// 其他平台（省市场局）是否有申请公章
			List<GsYwtb> gyList = gsYwtbService.findByBusinessLicense(loginInfo.getInstCode());
			if(gyList.size() > 0) {
				for(GsYwtb gy : gyList) {
					if(!StringUtils.equals("1", gy.getRefund())) {
						List<GsYwtbSeal> gysList = gsYwtbSealService.findByMainId(gy.getId());
						for(GsYwtbSeal gys : gysList) {
							if(StringUtils.equals(SealMedia.ELEC.getKey(), gys.getSealMedia())
									&& StringUtils.equals(SealType.TYPE_01.getKey(), gys.getSealType())) {
								throw new RequestSystemException("当前单位已存在申请公章的订单，无法重复申请");
							}
						}
					}
				}
			}
			// 系统内是否有公章
			SystemCustom sc = systemCustomService.findByBusinessLicenseForOne(loginInfo.getInstCode());
			if(sc != null) {
				String customId = sc.getId();
				String type = SealType.TYPE_01.getKey();
				List<SystemSeal> seals = systemSealService.findByCustomIdAndType(customId, type);
				if(seals.size() > 0) {
					for(SystemSeal seal : seals) {
						if(StringUtils.equals(SealType.TYPE_01.getKey(), seal.getType()) 
								&& StringUtils.equals(SealMedia.ELEC.getKey(), seal.getMediatype())
								&& StringUtils.indexOf("1234", seal.getStatus()) > -1) {
							throw new RequestSystemException("当前单位已存在备案公章");
						}
					}
				}
			}
		}
	}

	public String addElecOrder(AddElecOrder addElecOrder, LoginInfo loginInfo) throws Exception{
		// 信息入库到YWTB_CITY_ORDER
		YwtbCityOrder ywtbCityOrder = new YwtbCityOrder();
		String id = CommonUtil.createUuid();
		ywtbCityOrder.setBusinessId(loginInfo.getBusinessId());
		ywtbCityOrder.setPripid(loginInfo.getPripid());
		ywtbCityOrder.setCompanyId(addElecOrder.getCompanyId());
		ywtbCityOrder.setTradeno(addElecOrder.getTradeNo());
		ywtbCityOrder.setPay(Pay.PAY_NO.getKey());
		ywtbCityOrder.setPayChannel("wxappletpay");
		ywtbCityOrder.setDeliveryMethod(addElecOrder.getDeliveryMethod());
		//ywtbCityOrder.setZfsj(zfsj);
		ywtbCityOrder.setReceiveName(addElecOrder.getReceiveName());
		ywtbCityOrder.setReceiveCertType(CertType.SFZ.getKey());
		ywtbCityOrder.setReceiveCertNo(addElecOrder.getReceiveCertNo());
		ywtbCityOrder.setReceiveAddress(addElecOrder.getReceiveAddress());
		ywtbCityOrder.setReceivePhone(addElecOrder.getReceivePhone());
		ywtbCityOrder.setName(loginInfo.getName());
		ywtbCityOrder.setAddress(loginInfo.getAddress());
		ywtbCityOrder.setInstCode(loginInfo.getInstCode());
		ywtbCityOrder.setCrpName(loginInfo.getCrpName());
		ywtbCityOrder.setCrpCertType(CertType.SFZ.getKey());
		ywtbCityOrder.setCrpCertNo(loginInfo.getCrpCertNo());
		ywtbCityOrder.setCrpPhone(loginInfo.getCrpPhone());
		ywtbCityOrder.setApName(loginInfo.getApName());
		ywtbCityOrder.setApCertType(CertType.SFZ.getKey());
		ywtbCityOrder.setApCertNo(loginInfo.getApCertNo());
		ywtbCityOrder.setDistrictCode(loginInfo.getDistrictCode());
		ywtbCityOrder.setCreateDate(loginInfo.getCreateDate());
		ywtbCityOrder.setBz("0");
		ywtbCityOrder.setId(id);
		ywtbCityOrder.setGov("0");
		ywtbCityOrder.setESeal("1");
		//ywtbCityOrder.setGmtCreate();
		ywtbCityOrder.setCreateApp(loginInfo.getAppId());
		//ywtbCityOrder.setGmtUpdate();
		
		BigDecimal total = BigDecimal.ZERO;
		List<YwtbCityOrderSeal> ywtbCityOrderSeals = new ArrayList<YwtbCityOrderSeal>();
		List<TCompanyPrice> tCompanyPrices = tCompanyPriceService.findByCompanyId(addElecOrder.getCompanyId(), SealMedia.ELEC.getKey());
		for(AddElecOrderInnerSeal innerSeal : addElecOrder.getSeals()) {
			TCompanyPrice tCompanyPrice = tCompanyPrices.get(0);
			// 信息入库到YWTB_CITY_ORDER_SEAL
			YwtbCityOrderSeal ywtbCityOrderSeal = new YwtbCityOrderSeal();
			ywtbCityOrderSeal.setSealId(CommonUtil.createUuid());
			ywtbCityOrderSeal.setPripid(loginInfo.getPripid());
			ywtbCityOrderSeal.setSealType(innerSeal.getSealType());
			ywtbCityOrderSeal.setSealMedia(SealMedia.ELEC.getKey());
			ywtbCityOrderSeal.setSealMaterial(innerSeal.getSealMaterial());
			// 存储价格
			BigDecimal price = BigDecimal.ZERO;
			if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_01.getKey())) {
				price = tCompanyPrice.getSeal01();
			} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_02.getKey())) {
				price = tCompanyPrice.getSeal02();
			} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_03.getKey())) {
				price = tCompanyPrice.getSeal03();
			} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_04.getKey())) {
				price = tCompanyPrice.getSeal04();
			} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_05.getKey())) {
				price = tCompanyPrice.getSeal05();
			} else if(StringUtils.equals(innerSeal.getSealType(), SealType.TYPE_11.getKey())) {
				price = tCompanyPrice.getSeal06();
			} else {
				// 其他类型按公章价格
				price = tCompanyPrice.getSeal01();
			}
			//ywtbCityOrderSeal.setPrice(innerSeal.getPrice());
			ywtbCityOrderSeal.setPrice(price);
			ywtbCityOrderSeal.setBz("0");
			//ywtbCityOrderSeal.setGmtCreate(gmtCreate);
			ywtbCityOrderSeal.setId(CommonUtil.createUuid());
			ywtbCityOrderSeal.setMainId(id);
			// 电子印章有
			ywtbCityOrderSeal.setReferSealId(innerSeal.getReferSealId());
			total = total.add(price);
			ywtbCityOrderSeals.add(ywtbCityOrderSeal);
		}
		ywtbCityOrder.setTotal(total);
		ywtbCityOrderSealService.inserts(ywtbCityOrderSeals);
		ywtbCityOrderMapper.insert(ywtbCityOrder);
		return id;
	}
}
